package com.hunktimes.wholesale.ui.tabs.jewel

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.core.os.bundleOf
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import cn.bingoogolapple.bgabanner.BGABanner
import com.gyf.immersionbar.ImmersionBar
import com.hunktimes.wholesale.R
import com.hunktimes.wholesale.base.BaseMainFragment
import com.hunktimes.wholesale.databinding.FragmentHomeBinding
import com.hunktimes.wholesale.extension.setImageUrl
import com.hunktimes.wholesale.mock.banners
import com.hunktimes.wholesale.mock.MockJsonParser.jewels
import com.hunktimes.wholesale.net.model.BannerResp
import com.hunktimes.wholesale.net.model.MockGoods
import com.hunktimes.wholesale.ui.tabs.cart.CartRecommendAdapter
import com.hunktimes.wholesale.util.Constants
import com.hunktimes.wholesale.util.L

/**
 * Author: Delete
 * Date: 2020/9/1 09:50
 * Description:
 *
 */
class JewelFragment : BaseMainFragment(R.layout.fragment_home) {

    lateinit var viewBinding: FragmentHomeBinding

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewBinding = FragmentHomeBinding.bind(view)

    }

    /**
     * 1.在布局文件的根节点使用android:fitsSystemWindows="true"属性
     * 2.然后使用ImmersionBar时候必须指定状态栏颜色 及 开枪 DarkFont
     */
    override fun initImmersionBar() {
        ImmersionBar.with(this)
            .statusBarDarkFont(true)
            .statusBarColor(R.color.white)
            .init()
    }

    override fun onStart() {
        super.onStart()
        //initbanner()
        viewBinding.searchEt.setOnClickListener {
            rootNavController.navigate(R.id.navigation_search)
        }
        initSaleAdapter()
        initHotAdapter()
        initRecommendAdapter()
        initNewsAdapter()
    }

    /**
     * banner 配置
     */
    val bannerAdapter = object : BGABanner.Adapter<ImageView, BannerResp> {
        override fun fillBannerItem(
            banner: BGABanner?,
            itemView: ImageView?,
            model: BannerResp?,
            position: Int
        ) {
            itemView?.setImageUrl(model?.img!!)
        }

    }

    fun initbanner() {
//        val data = arrayListOf<BannerResp>(
//            BannerResp("http://pic1.win4000.com/wallpaper/8/583fbfbc43be1.jpg"),
//            BannerResp("http://pic1.win4000.com/wallpaper/8/583fbfbdb09dd.jpg"),
//            BannerResp("http://pic1.win4000.com/wallpaper/8/583fbfbf301e4.jpg")
//        )
        val data = banners.map { BannerResp(it) }

//        viewBinding.banner.apply {
//            setAdapter(bannerAdapter)
//            setData(data, null)
//            viewBinding.banner.setDelegate { _, _, bannerResp, _ ->
//                L.d("Banner click -> $bannerResp")
//            }
//        }

    }

    val saleAdapter = CartRecommendAdapter()
    fun initSaleAdapter() {
        val saleLm = GridLayoutManager(requireContext(), 3)
        saleLm.orientation = LinearLayoutManager.VERTICAL
        viewBinding.homeSaleRcv.layoutManager = saleLm
        viewBinding.homeSaleRcv.adapter = saleAdapter

        val data = mutableListOf<MockGoods>()
//        for (i in 1..3) {
//            data.add("首頁熱門$i")
//        }
        repeat(3) {
            data.add(jewels.random())
        }
        saleAdapter.data = data
        saleAdapter.setOnItemClickListener { adapter, view, position ->
            L.d("saleAdapter.setOnItemChildClickListener")
            val bundle = bundleOf(Constants.key_goods_bundle to adapter.data[position])
            rootNavController.navigate(R.id.navigation_product,bundle)
        }
    }


    val hotAdapter = CartRecommendAdapter()
    fun initHotAdapter() {
        val hotLm = GridLayoutManager(requireContext(), 3)
        hotLm.orientation = LinearLayoutManager.VERTICAL
        viewBinding.homeHotRcv.layoutManager = hotLm
        viewBinding.homeHotRcv.adapter = hotAdapter

        val data = mutableListOf<MockGoods>()
//        for (i in 1..3) {
//            data.add("首頁熱門$i")
//        }
        repeat(3) {
            data.add(jewels.random())
        }
        hotAdapter.data = data
        hotAdapter.setOnItemClickListener { adapter, view, position ->
            L.d("hotAdapter.setOnItemChildClickListener")
            val bundle = bundleOf(Constants.key_goods_bundle to adapter.data[position])
            rootNavController.navigate(R.id.navigation_product,bundle)
        }
    }

    val recommendAdapter = CartRecommendAdapter()
    fun initRecommendAdapter() {
        val recommendLm = GridLayoutManager(requireContext(), 3)
        recommendLm.orientation = LinearLayoutManager.VERTICAL
        viewBinding.homeRecommendRcv.layoutManager = recommendLm
        viewBinding.homeRecommendRcv.adapter = recommendAdapter

        val data = mutableListOf<MockGoods>()
//        for (i in 1..3) {
//            data.add("首頁推薦$i")
//        }
        repeat(3) {
            data.add(jewels.random())
        }
        recommendAdapter.data = data
    }

    val newAdapter = CartRecommendAdapter()
    fun initNewsAdapter() {
        val newsLm = GridLayoutManager(requireContext(), 3)
        newsLm.orientation = LinearLayoutManager.VERTICAL
        viewBinding.homeNewRcv.layoutManager = newsLm
        viewBinding.homeNewRcv.adapter = newAdapter

        val data = mutableListOf<MockGoods>()
//        for (i in 1..3) {
//            data.add("首頁新品$i")
//        }
        repeat(3) {
            data.add(jewels.random())
        }
        newAdapter.data = data
    }
}